﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.OleDb;
using ESOL.Business.Hr;
using ESOL.Common.Entity;
using System.IO;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainAttendanceImport : BaseForm.frmBaseDialogyForm
    {
        public frmMaintainAttendanceImport()
        {
            InitializeComponent();
        }

        private bool CheckFileExist()
        {
            if (string.IsNullOrEmpty(txtSelectedFile.Text))
            {
                return false;
            }
            return File.Exists(txtSelectedFile.Text);
        }

        private DataTable GetDataTableFromExcel()
        {
            string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + txtSelectedFile.Text + ";Extended Properties=Excel 8.0";
            OleDbConnection conn = new OleDbConnection(strCon);
            string tableName = "考勤日报表$";
            conn.Open();
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            if (dt != null && dt.Rows.Count > 0)
            {
                tableName = dt.Rows[0]["TABLE_NAME"].ToString();
            }
            string sql = "select * from [" + tableName + "]"; 
            OleDbDataAdapter cmd = new OleDbDataAdapter(sql, strCon); 
            DataSet ds = new DataSet();
            cmd.Fill(ds, "Attendance"); 
            conn.Close();
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckFileExist() == false)
                {
                    MessageBox.Show("此文件不存在，请重新选择！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DataTable dt = GetDataTableFromExcel();
                if (dt == null || dt.Rows.Count == 0)
                {
                    MessageBox.Show("此文件中没有数据，请重新选择！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                pbarProgress.Properties.Minimum = 0;
                pbarProgress.Properties.Maximum = dt.Rows.Count;
                pbarProgress.Properties.Step = 1;
                pbarProgress.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid;
                bool successful = true;
                foreach (DataRow dr in dt.Rows)
                {
                    successful = HrAttendanceRecordBs.ImportAttendance(dr);
                    if (!successful)
                    {
                        break;
                    }
                    pbarProgress.PerformStep();
                }
                if (successful)
                {
                    MessageBox.Show("导入成功，共导入" + dt.Rows.Count.ToString() + "条记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("导入失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtSelectedFile_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Excel文件|*.xls";
            fd.Multiselect = false;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                txtSelectedFile.Text = fd.FileName;
            }
        }
    }
}
